//
//  DeliveryAddressCell.m
//  bestkeep
//
//  Created by bj on 2016/11/4.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BK_my_ShippingAddressCell.h"
#import "Masonry.h"
#import "BKMyCommon.h"


#define ShippingAddress_BackGauge   ApplicationBackGauge
@implementation BK_my_ShippingAddressCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}

- (void)createUI {
    UIView *smallBgView = [[UIView alloc] init];
    _bgImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _bgImageView.userInteractionEnabled = YES;
    _bgImageView.backgroundColor = RGBCOLOR(238, 238, 238, 1);
    [self.contentView addSubview:_bgImageView];
    [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).mas_offset(0);
        make.left.equalTo(self.mas_left);
        make.bottom.equalTo(self.mas_bottom).mas_offset(0);
        make.right.equalTo(self.mas_right);
    }];
    
    [_bgImageView addSubview:smallBgView];
    smallBgView.backgroundColor = [UIColor whiteColor];
    [smallBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgImageView.mas_top).mas_offset(5);
        make.right.equalTo(_bgImageView.mas_right);
        make.left.equalTo(_bgImageView.mas_left);
        make.bottom.equalTo(_bgImageView.mas_bottom).mas_offset(-5);
    }];
    
    //姓名 +  电话号码   背景View
    UIView *backView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 36)];
    backView1.backgroundColor = [UIColor clearColor];
    [smallBgView addSubview:backView1];
    
    //收货地址  背景View
    UIView *backView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 36, ScreenWidth, 50)];
    backView2.backgroundColor = [UIColor clearColor];
    [smallBgView addSubview:backView2];
    
    //背景View
    self.deleteAndDefaultButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 86, ScreenWidth, 36)];
    self.deleteAndDefaultButtonView.backgroundColor = [UIColor clearColor];
    [smallBgView addSubview:self.deleteAndDefaultButtonView];
    
    self.nameLabel = [UILabel new];
    self.telephoneLabel = [UILabel new];
    self.addressLabel = [UILabel new];
    [backView1 addSubview:self.nameLabel];
    [backView1 addSubview:self.telephoneLabel];
    [backView2 addSubview:self.addressLabel];

    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.textColor = BK_MY_COLOR_07;
    self.telephoneLabel.font = [UIFont systemFontOfSize:15];
    self.telephoneLabel.textColor = BK_MY_COLOR_07;
    self.addressLabel.font = [UIFont systemFontOfSize:14];
    self.addressLabel.textColor = BK_MY_COLOR_02;
    self.addressLabel.numberOfLines = 2;
    
    self.lineView = [CALayer layer];
    self.lineView.frame = CGRectMake(15, 86, ScreenWidth - 15, 1);
    self.lineView.opacity = 0.15;
    self.lineView.backgroundColor = BK_MY_COLOR_05.CGColor;
    [smallBgView.layer addSublayer:self.lineView];
  
    //删除按钮
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    self.deleteButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    [self.deleteButton setTitleColor:BK_MY_COLOR_04 forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteShippingAddressButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteAndDefaultButtonView addSubview:self.deleteButton];
    self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:14];
    UIImageView *deleteImg = [[UIImageView alloc] initWithFrame:CGRectZero];
    deleteImg.image = [UIImage imageNamed:@"BK_my_addressDeleteButton"];
    [self.deleteButton addSubview:deleteImg];
  
    //编辑按钮
    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.editButton setTitle:@"编辑" forState:UIControlStateNormal];
    self.editButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    [self.editButton setTitleColor:BK_MY_COLOR_04 forState:UIControlStateNormal];
    self.editButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.editButton addTarget:self action:@selector(editShippingAddressButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteAndDefaultButtonView addSubview:self.editButton];
    UIImageView *editImg = [[UIImageView alloc] initWithFrame:CGRectZero];
    editImg.image = [UIImage imageNamed:@"BK_my_addressEditButton"];
    [self.editButton addSubview:editImg];
    
    //设置默认地址按钮
    self.set_DefaultAddressButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.set_DefaultAddressButton addTarget:self action:@selector(setUpdefaultAddressButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteAndDefaultButtonView addSubview:self.set_DefaultAddressButton];
    [self.set_DefaultAddressButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.deleteAndDefaultButtonView.mas_left).mas_offset(ShippingAddress_BackGauge);
        make.centerY.equalTo(self.deleteAndDefaultButtonView.mas_centerY);
        make.height.equalTo(@(17));
        make.width.equalTo(@(17));
    }];

    self.set_DefaultAddressLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self.deleteAndDefaultButtonView addSubview:self.set_DefaultAddressLabel];
    self.set_DefaultAddressLabel.font = [UIFont systemFontOfSize:14.0];
    [self.set_DefaultAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.set_DefaultAddressButton.mas_right).mas_offset(5);
        make.centerY.equalTo(self.set_DefaultAddressButton.mas_centerY);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView1.mas_left).mas_offset(ShippingAddress_BackGauge);
        make.centerY.equalTo(backView1.mas_centerY);
        make.right.equalTo(self.telephoneLabel.mas_left).offset(30);
        make.height.equalTo(@(20));
    }];

    /*
     * 电话号码
     */
//    self.telephoneLabel.backgroundColor=[UIColor  yellowColor];
    [self.telephoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).mas_offset(-ShippingAddress_BackGauge);
        make.centerY.equalTo(backView1.mas_centerY);
    }];

    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView2.mas_left).mas_offset(ShippingAddress_BackGauge);
        make.width.equalTo(@(ScreenWidth - 2*ShippingAddress_BackGauge));
        make.centerY.equalTo(backView2.mas_centerY).mas_offset(-5);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).mas_offset(-ShippingAddress_BackGauge);
        make.width.equalTo(@(60));
        make.height.equalTo(@(24));
        make.centerY.equalTo(self.deleteAndDefaultButtonView.mas_centerY);
    }];

    [deleteImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deleteButton.mas_left).mas_offset(20);
        make.centerY.equalTo(self.deleteButton.mas_centerY);
    }];

    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(deleteImg.mas_left).mas_offset(-10);
        make.width.equalTo(@(60));
        make.height.equalTo(@(24));
        make.centerY.equalTo(self.deleteAndDefaultButtonView.mas_centerY);
    }];
    
    [editImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.editButton.mas_left).mas_offset(20);
        make.centerY.equalTo(self.editButton.mas_centerY);
    }];
}

- (void)setUpdefaultAddressButton:(UIButton *)click {
        if ([self.addressButtonDelegate respondsToSelector:@selector(clickSetAddressCell:selectedButton:)]) {
            [self.addressButtonDelegate clickSetAddressCell:self selectedButton:click];
        }
}

- (void)editShippingAddressButton:(UIButton *)click {
    if ([self.addressButtonDelegate respondsToSelector:@selector(clickEditAddressButton:addressModel::)]) {
        [self.addressButtonDelegate clickEditAddressButton:click addressModel:self.datas :self];
    }
}

- (void)deleteShippingAddressButton:(UIButton *)click {
    if ([self.addressButtonDelegate respondsToSelector:@selector(clickDelegateAddress:)]) {
        [self.addressButtonDelegate clickDelegateAddress:self.datas];
    }
}

- (void)updateCellInformation:(BK_my_ShippingAddress *)address {
    self.datas = address;
    self.nameLabel.text = address.consigneeName;
    self.telephoneLabel.text = address.shippingTelephone;
    self.addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@", address.shippingProvinceName, address.shippingCityName, address.shippingCountyName, address.shippingDetailAddress];
    if (address.isDefaultAddress) {
        self.set_DefaultAddressLabel.text = @"默认地址";
        self.set_DefaultAddressLabel.textColor = BK_MY_COLOR_07;
        /*
         * 彩边黄底
         */
        self.bgImageView.image = [UIImage imageNamed:@"BKOrder_address_BG"];
        
        /*
         * 彩边白底
         */
//        self.bgImageView.image = [UIImage imageNamed:@"BK_my_addressDefaultBgImg"];
        
        
        
        [self.set_DefaultAddressButton setImage:[UIImage imageNamed:@"BK_my_addressDefaultSelected"] forState:UIControlStateNormal];
        self.isDefault = YES;

    } else {
        self.bgImageView.image = nil;
        self.set_DefaultAddressLabel.textColor = BK_MY_COLOR_04;
        self.set_DefaultAddressLabel.text = @"设为默认地址";
        [self.set_DefaultAddressButton setImage:[UIImage imageNamed:@"BK_my_addressUnSelected"] forState:UIControlStateNormal];
        self.isDefault = NO;
    }
}

@end
